package code.D1222;

/**
 * author： yeswater
 * create： 2023/12/22
 * <p>
 * 把二叉搜索树转换为累加树
 */
public class T538 {
    int tot = 0;
    public TreeNode convertBST(TreeNode root) {
        dfs(root);
        return root;
    }
    
    private void dfs(TreeNode root) {
        if (root == null) {
            return;
        }
        dfs(root.right);
        tot += root.val;
        root.val = tot;
        dfs(root.left);
    }
    
    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        
        TreeNode() {
        }
        
        TreeNode(int val) {
            this.val = val;
        }
        
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
